package com.bingbaihanji.cloud.controller;

import com.bingbaihanji.cloud.entities.Pay;
import com.bingbaihanji.cloud.entities.PayDTO;
import com.bingbaihanji.cloud.resp.ResultData;
import com.bingbaihanji.cloud.resp.ReturnCodeEnum;
import com.bingbaihanji.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 冰白寒祭
 * @date 2024-03-06 14:13:43
 * @description //TODO
 */
@RestController
@Slf4j
@Tag(name = "支付微服务模块", description = "支付CRUD")
public class PayController {

    @Value("${server.port}")
    private String port;

    @Resource
    private PayService payService;

    @PostMapping("/pay/add")
    @Operation(summary = "新增", description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        log.info("Pay的值为{}", pay.toString());
        var add = payService.add(pay);
        return ResultData.success("成功插入记录,返回值为" + add);
    }

    @DeleteMapping("/pay/del/{id}")
    @Operation(summary = "删除", description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {

        var i = payService.delete(id);

        return ResultData.success(i);
    }

    @PutMapping("/pay/update")
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        log.info("PayDTO的值为{}", payDTO.toString());
        var pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        return ResultData.success("成功修改记录,返回值为" + payService.update(pay));
    }

    @GetMapping("/pay/get/{id}")
    @Operation(summary = "按照ID查流水", description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        if (id < 0) {
            throw new RuntimeException("id不能为负数");
        }
        var pay = payService.getById(id);

        try {
            Thread.sleep(62 * 1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        if (pay == null) {
            return ResultData.fail(ReturnCodeEnum.RC500.getCode());
        }
        log.info("查询成功Pay的值为{}", pay);
        return ResultData.success(pay);
    }

    @GetMapping("/pay/getall")
    @Operation(summary = "查询所有流水", description = "查询支付流水所有方法")

    public ResultData<List<Pay>> getAll() {
        return ResultData.success(payService.getAll());
    }

    @GetMapping("/pay/get/info")
    public String getInfoByConsul(@Value("${bingbaihanji.info}") String bingbaihanjiInfo) {
//        System.out.println(bingbaihanjiInfo);
        return "bingbaihanjiInfo:" + bingbaihanjiInfo + "\t" + "port:" + port;

    }


}
